package cn.tedu.learn.tea.domain.service.impl;

import cn.tedu.learn.tea.domain.bo.ContentBO;
import cn.tedu.learn.tea.domain.bo.ContentDetailBO;
import cn.tedu.learn.tea.domain.bo.ContentListBO;
import cn.tedu.learn.tea.domain.common.exception.BusinessException;
import cn.tedu.learn.tea.domain.repository.ContentDetailRepository;
import cn.tedu.learn.tea.domain.repository.ContentRepository;
import cn.tedu.learn.tea.domain.response.StatusCode;
import cn.tedu.learn.tea.domain.service.ContentService;
import cn.tedu.learn.tea.protocol.parm.ContentParam;
import cn.tedu.learn.tea.protocol.parm.ContentUpdateParam;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 内容实现类
 */
@Service
@Slf4j
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentRepository contentRepository;

    @Autowired
    private ContentDetailRepository contentDetailRepository;

    @Value("${url}")
    private String url;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addContent(ContentParam contentParam) {
        log.debug("传入的新增内容参数为:{}", contentParam);
        //1.插入Content内容
        String currentUrl = contentParam.getCoverUrl();
        contentParam.setCoverUrl(currentUrl.contains(url) ? currentUrl.split(url)[1] : currentUrl);
        Integer contentId = contentRepository.insert(contentParam);

        //新增内容失败，抛出异常
        if (contentId == null) {
            throw new BusinessException(StatusCode.APP_ERROR, "新增内容失败");
        }

        //2.插入内容详情
        contentParam.setId(contentId);
        Integer contentDetailCount = contentDetailRepository.insert(contentParam);

        //新增内容详情失败，抛出异常
        if (contentDetailCount.equals(0)) {
            throw new BusinessException(StatusCode.APP_ERROR, "新增内容失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContent(ContentUpdateParam contentUpdateParam) {
        log.debug("传入的修改内容参数为:{}", contentUpdateParam);
        //1.判断当前要修改的内容是否存在
        int contentCount = contentRepository.countContentById(contentUpdateParam.getId());
        if (contentCount == 0) {
            throw new BusinessException(StatusCode.APP_ERROR, "要修改的内容不存在");
        }

        //2.修改内容
        String currentUrl = contentUpdateParam.getCoverUrl();
        contentUpdateParam.setCoverUrl(currentUrl.contains(url) ? currentUrl.split(url)[1] : currentUrl);
        Integer contentRows = contentRepository.update(contentUpdateParam);

        //3.修改内容详情
        int contentDetailRows = contentDetailRepository.update(contentUpdateParam);

        //4.如果有一个修改不成功，则抛出异常并回滚
        if (contentRows == 0 || contentDetailRows == 0) {
            throw new BusinessException(StatusCode.APP_ERROR, "修改内容失败");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContent(Integer contentId) {
        log.debug("传入的删除内容参数为:{}", contentId);
        //1.判断当前要删除的内容是否存在
        int contentCount = contentRepository.countContentById(contentId);
        if (contentCount == 0) {
            throw new BusinessException(StatusCode.APP_ERROR, "要删除的内容不存在");
        }

        //2.删除内容
        int contentRows = contentRepository.delete(contentId);

        //3.删除内容详情
        int contentDetailRows = contentDetailRepository.delete(contentId);

        //4.如果有一个删除不成功，则抛出异常并回滚
        if (contentRows == 0 || contentDetailRows == 0) {
            throw new BusinessException(StatusCode.APP_ERROR, "删除内容失败");
        }
    }

    @Override
    public PageInfo<ContentListBO> list(Integer page, Integer pageSize) {
        //分页查询
        PageInfo<ContentListBO> contentBoList = contentRepository.list(page, pageSize);
        return contentBoList;
    }

    @Override
    public ContentBO selectContentById(Integer contentId) {
        //1.判断当前要查询的内容是否存在
        int contentCount = contentRepository.countContentById(contentId);
        if (contentCount == 0) {
            throw new BusinessException(StatusCode.APP_ERROR, "要查询的内容不存在");
        }

        //2.查询内容信息
        ContentBO contentBO = contentRepository.selectContentById(contentId);

        //3.查询内容详情
        ContentDetailBO contentDetailBO = contentDetailRepository.selectContentDetailById(contentId);

        //4.封装内容BO
        contentBO.setDetail(contentDetailBO.getDetail());

        return contentBO;
    }
}
